package com.fenghuajueli.moduledev;

import androidx.appcompat.app.AppCompatDelegate;

import com.blankj.utilcode.util.GsonUtils;
import com.fenghuajueli.libbasecoreui.BaseApplication;
import com.fenghuajueli.libbasecoreui.constants.AppConfigInfo;
import com.fenghuajueli.libbasecoreui.constants.PrivacyConstantsUtils;
import com.fenghuajueli.libbasecoreui.utils.MmkvUtils;
import com.fenghuajueli.module_user.model.RequestInfoModel;
import com.fenghuajueli.module_user.utils.UserModuleInitUtils;
import com.google.gson.JsonObject;
import com.google.gson.JsonParser;
import com.ijianji.module_play_video.PlayVideoModuleInitUtils;
import com.tencent.bugly.crashreport.CrashReport;



/**
 * ===========================================================
 * author：songpeng on 1/14/21 16:05
 * email：fenghuajueli@gmail.com
 * ===========================================================
 */
public class MyApplication extends BaseApplication {


    /**
     * 亮/暗夜切换主题
     */
    @Override
    @AppCompatDelegate.NightMode
    public int getDayNightMode() {
        return AppCompatDelegate.MODE_NIGHT_NO;
    }


    /**
     * 在这里对app做正常的初始化操作
     */
    @Override
    public void initNormalSdk() {
        super.initNormalSdk();
        initAppInfo();
        initPrivacy();

        //视频模块也先去除掉 根据项目需要在添加模块依赖
        PlayVideoModuleInitUtils.init(this);
    }

    /**
     * 初始化第三方的sdk，有要获取imei mac地址 正在运行的应用程序等这些隐私信息的 都需要在这里做处理
     */
    @Override
    public void initThirdSdk() {
        super.initThirdSdk();
        //用户系统模块在第一个版本可以去除掉，防止因为检测出对应的sdk 和未在隐私协议里添加说明导致审核不通过
        UserModuleInitUtils.init(this);
        CrashReport.initCrashReport(getApplicationContext(), AppConfigInfo.BUGLY_APPID, false);
    }


    /**
     * app相关信息初始化
     */
    private void initAppInfo() {
        AppConfigInfo.APPLICATION_ID = BuildConfig.APPLICATION_ID;
        AppConfigInfo.APP_COMPANY = BuildConfig.COMPANY_NAME;
        AppConfigInfo.APP_DEBUG = BuildConfig.DEBUG;
        AppConfigInfo.APP_NAME = BuildConfig.APP_NAME;
        AppConfigInfo.CHANNEL = BuildConfig.FLAVOR;
        AppConfigInfo.VERSION_CODE = BuildConfig.VERSION_CODE;
        AppConfigInfo.ALI_LOGIN_KEY = BuildConfig.ALI_LOGIN_KEY;
        AppConfigInfo.WECHAT_APP_ID = BuildConfig.WX_APP_ID;
        AppConfigInfo.WECHAT_APP_SECRET = BuildConfig.WX_APP_SECRET;
        AppConfigInfo.BASE_DEBUG_URL = "https://" + BuildConfig.HTTP_HOST + "/api/v1_test/";
        AppConfigInfo.BASE_RELEASE_URL = "https://" + BuildConfig.HTTP_HOST + "/api/v1/";
        AppConfigInfo.HTTP_HOST = BuildConfig.HTTP_HOST;
        AppConfigInfo.isShowBuyAgreement = false;  //是否在条款协议页面显示购买须知
        AppConfigInfo.isShowSubscribeAgreement = false; //是否在条款协议页面显示订阅协议
        //新版本给出了通用的订阅协议，这里默认填写空字符串，如果需要特殊的订阅协议，则这里填写订阅协议的地址
        AppConfigInfo.SUBSCRIBE_AGREEMENT_URL = "";
        AppConfigInfo.PRIVACY_URL = BuildConfig.PRIVACY_URL;
        AppConfigInfo.BUGLY_APPID = BuildConfig.BUGLY_APPID;
    }

    /**
     * 隐私协议相关初始化
     */
    private void initPrivacy() {
        PrivacyConstantsUtils.appName = BuildConfig.APP_NAME;
        PrivacyConstantsUtils.companyName = BuildConfig.COMPANY_NAME;
        PrivacyConstantsUtils.vipContent = BuildConfig.VIP_CONTENT;
        PrivacyConstantsUtils.vipDesc = BuildConfig.VIP_BUY_DESC;
        //将隐私政策的配置数据保存到本地  PrivacyWebActivity读取对应的值来显示
        JsonObject jsonObject = new JsonObject();
        jsonObject.add("permissionDescription", new JsonParser().parse(BuildConfig.PERMISSION_DATA));
        jsonObject.add("policyList", new JsonParser().parse(BuildConfig.SDK_DATA));
        MmkvUtils.save("WEB_PRIVACY_DATA", GsonUtils.toJson(jsonObject));
        // 默认关闭游客支付能力，这里设为false表示打开游客支付
        MmkvUtils.save(RequestInfoModel.LOGIN_OUT, true);
    }

    @Override
    public boolean appIsDebug() {
        return BuildConfig.DEBUG;
    }

    @Override
    public String getChannel() {
        return BuildConfig.FLAVOR;
    }

    @Override
    public String getHttpHost() {
        return BuildConfig.HTTP_HOST;
    }
}
